﻿/*!	@file	r_draw_context_dx11.cpp
	@brief	描画コンテキスト
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "r_draw_context_dx11.h"
#if R_TARGET_UNIQUEIS(R_TARGET_UNIQUE_WINDX11)

#include "../../r_debug.h"

#include "../../math/r_math_colorRGBf.h"
#include "r_draw_texture2d_dx11.h"
#include "r_draw_sampler_dx11.h"
#include "r_draw_rasterizerState_dx11.h"
#include "r_draw_depthstencil_dx11.h"
#include "r_draw_rendertarget_dx11.h"

#include "r_draw_blendState_dx11.h"
#include "r_draw_depthstencilState_dx11.h"

#include "r_draw_inputlayout_dx11.h"
#include "r_draw_vertexbuffer_dx11.h"
#include "r_draw_indexbuffer_dx11.h"
#include "r_draw_constantbuffer_dx11.h"

#include "r_draw_vertexshader_dx11.h"
#include "r_draw_pixelshader_dx11.h"

#include "r_draw_core_dx11.h"

// >>>>>>>>>> RTTI >>>>>>>>>>
#include "../../rtti/r_rtti_reflection_implement.h"
namespace r{
	namespace draw{
		namespace dx11{
			RRTTI_REFLECTION_IMPLEMENT ( r::draw::dx11::ContextDX11, r::draw::Context )

			// <<<<<<<<<< RTTI <<<<<<<<<<

			ContextDX11 :: ContextDX11()
			{
			}

			ContextDX11 :: ~ContextDX11()
			{
			}

			void ContextDX11 :: ClearRenderTargetView ( const r::draw::RenderTarget* renderTarget, const r::math::ColorRGBf& color )
			{
				// 仮に逐一実行します
				r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->ClearRenderTargetView (
					r::rtti::dynamicCast<const RenderTargetDX11*> ( renderTarget )->getRenderTarget(), color.rgba );
			}

			void ContextDX11 :: ClearDepthStencilView ( const r::draw::DepthStencil* depthStencil, r_ui32 flag, r_f32 depth, r_ui32 stencil )
			{
				// 仮に逐一実行します
				r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->ClearDepthStencilView (
					r::rtti::dynamicCast<const DepthStencilDX11*> ( depthStencil )->getDepthStencil(), flag, depth, stencil );
			}

			void ContextDX11 :: OM_SetRenderTargets ( r_ui32 numTarget, const r::draw::RenderTarget* renderTarget, const r::draw::DepthStencil* depthStencil )
			{
				// 仮に逐一実行します
				const r_ui32				maxTarget = 16;
				ID3D11RenderTargetView*		target[maxTarget];
				if ( numTarget > maxTarget ) {
					numTarget = maxTarget;
				}
				for ( r_ui32 i = 0; i < numTarget; ++i ) {
					target[i] = r::rtti::dynamicCast<const RenderTargetDX11*> ( renderTarget )->getRenderTarget();
				}
				r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->OMSetRenderTargets ( numTarget, target, r::rtti::dynamicCast<const DepthStencilDX11*> ( depthStencil )->getDepthStencil() );
			}

			void ContextDX11 :: OM_SetBlendState ( const r::draw::BlendState* state, r_f32	blendfuctor[4] )
			{
				// 仮に逐一実行します
				r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->OMSetBlendState ( r::rtti::dynamicCast<const BlendStateDX11*> ( state )->getBlendState(), blendfuctor, 0xffffffff );
			}

			void ContextDX11 :: OM_SetDepthStencilState ( const r::draw::DepthStencilState* state, r_ui32 stencilref )
			{
				// 仮に逐一実行します
				r::rtti::dynamicCast<CoreDX11*> ( Core::getInstance() )->getD3DContext()->OMSetDepthStencilState ( r::rtti::dynamicCast<const DepthStencilStateDX11*> ( state )->getDepthStencilState(), stencilref );
			}

			void ContextDX11 :: RS_SetViewPorts ( r_ui32 numViewPort, const r::draw::ViewPortInfo* viewPort )
			{
				// 仮に逐一実行します
				const r_ui32				maxViewPort = 16;
				D3D11_VIEWPORT				vp[maxViewPort];
				if ( numViewPort > maxViewPort ) {
					numViewPort = maxViewPort;
				}
				for ( r_ui32 i = 0; i < numViewPort; ++i ) {
					vp[i].Width				= viewPort[i].width;
					vp[i].Height			= viewPort[i].height;
					vp[i].MinDepth			= viewPort[i].minDepth;
					vp[i].MaxDepth			= viewPort[i].maxDepth;
					vp[i].TopLeftX			= viewPort[i].x;
					vp[i].TopLeftY			= viewPort[i].y;
				}
				r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->RSSetViewports ( numViewPort, vp );
			}

			void ContextDX11 :: RS_SetState ( const r::draw::RasterizerState* raster )
			{
				if ( raster == NULL ) {
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->RSSetState ( NULL );
				} else {
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->RSSetState ( r::rtti::dynamicCast<const RasterizerStateDX11*> ( raster )->getRasterizerState() );
				}
			}

			void	ContextDX11 :: IA_SetIndexBuffer ( const r::draw::IndexBuffer* buffer, r_ui32 offset )
			{
				// 仮に逐一実行します
				const IndexBufferDX11*	tbuffer		= r::rtti::dynamicCast<const IndexBufferDX11*> ( buffer );
				DXGI_FORMAT				tbuffFormat	= tbuffer->isLongBuffer() != false ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R16_UINT;
				r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->IASetIndexBuffer ( tbuffer->getIndexBuffer(), tbuffFormat, offset );
			}

			void	ContextDX11 :: IA_SetVertexBuffer ( r_ui32 slot, const r::draw::VertexBuffer* buffer, r_ui32 offset )
			{
				// 仮に逐一実行します
				const VertexBufferDX11* tbuffer	= r::rtti::dynamicCast<const VertexBufferDX11*> ( buffer );
				ID3D11Buffer*	tbuffArr[1]		= {tbuffer->getVertexBuffer() };
				r_ui32			tbuffStride		= tbuffer->getStride();
				r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->IASetVertexBuffers ( slot, 1, tbuffArr, &tbuffStride, &offset );
			}

			void	ContextDX11 :: IA_SetInputLayout ( const r::draw::InputLayout* layout )
			{
				// 仮に逐一実行します
				r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->IASetInputLayout ( r::rtti::dynamicCast<const InputLayoutDX11*> ( layout )->getLayout() );
			}

			void	ContextDX11 :: IA_SetPrimitiveTopology ( r_ui32 toporogy )
			{
				// 仮に逐一実行します
				r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->IASetPrimitiveTopology (
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->convertPrimitiveToporogy ( toporogy ) );
			}

			void	ContextDX11 :: VS_SetShader ( const r::draw::VertexShader *shader )
			{
				// 仮に逐一実行します
				r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->VSSetShader (
					r::rtti::dynamicCast<const VertexShaderDX11*> ( shader )->getShader(), NULL, 0 );
			}

			void	ContextDX11 :: VS_SetTexture2D ( r_ui32 slot, const r::draw::Texture2D* texture )
			{
				// 仮に逐一実行します
				if ( texture == NULL ) {
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->VSSetShaderResources ( slot, 1, NULL );
				} else {
					ID3D11ShaderResourceView*	resource[1] = {r::rtti::dynamicCast<const Texture2DDX11*> ( texture )->getResourceView() };
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->VSSetShaderResources ( slot, 1, resource );
				}
			}

			void	ContextDX11 :: VS_SetSampler ( r_ui32 slot, const r::draw::Sampler* sampler )
			{
				// 仮に逐一実行します
				if ( sampler == NULL ) {
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->VSSetSamplers ( slot, 1, NULL );
				} else {
					ID3D11SamplerState*			samplers[1] = {r::rtti::dynamicCast<const SamplerDX11*> ( sampler )->getSampler() };
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->VSSetSamplers ( slot, 1, samplers );
				}
			}

			void	ContextDX11 :: VS_SetConstantBuffer ( r_ui32 slot, const r::draw::ConstantBuffer* buffer )
			{
				// 仮に逐一実行します
				if ( buffer == NULL ) {
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->VSSetConstantBuffers ( slot, 1, NULL );
				} else {
					ID3D11Buffer*				constbuffer[1] = {r::rtti::dynamicCast<const ConstantBufferDX11*> ( buffer )->getConstantBuffer() };
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->VSSetConstantBuffers ( slot, 1, constbuffer );
				}
			}

			void	ContextDX11 :: PS_SetShader ( const r::draw::PixelShader *shader )
			{
				// 仮に逐一実行します
				r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->PSSetShader (
					r::rtti::dynamicCast<const PixelShaderDX11*> ( shader )->getShader(), NULL, 0 );
			}

			void	ContextDX11 :: PS_SetTexture2D ( r_ui32 slot, const r::draw::Texture2D* texture )
			{
				// 仮に逐一実行します
				if ( texture == NULL ) {
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->PSSetShaderResources ( slot, 1, NULL );
				} else {
					ID3D11ShaderResourceView*	resource[1] = {r::rtti::dynamicCast<const Texture2DDX11*> ( texture )->getResourceView() };
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->PSSetShaderResources ( slot, 1, resource );
				}
			}

			void	ContextDX11 :: PS_SetSampler ( r_ui32 slot, const r::draw::Sampler* sampler )
			{
				// 仮に逐一実行します
				if ( sampler == NULL ) {
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->PSSetSamplers ( slot, 1, NULL );
				} else {
					ID3D11SamplerState*			samplers[1] = {r::rtti::dynamicCast<const SamplerDX11*> ( sampler )->getSampler() };
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->PSSetSamplers ( slot, 1, samplers );
				}
			}

			void	ContextDX11 :: PS_SetConstantBuffer ( r_ui32 slot, const r::draw::ConstantBuffer* buffer )
			{
				// 仮に逐一実行します
				if ( buffer == NULL ) {
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->PSSetConstantBuffers ( slot, 1, NULL );
				} else {
					ID3D11Buffer*				constbuffer[1] = {r::rtti::dynamicCast<const ConstantBufferDX11*> ( buffer )->getConstantBuffer() };
					r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->PSSetConstantBuffers ( slot, 1, constbuffer );
				}
			}


			void	ContextDX11 :: draw ( r_ui32 numVertex, r_ui32 offset )
			{
				// 仮に逐一実行します
				r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->Draw ( numVertex, offset );
			}

			void	ContextDX11 :: drawIndex ( r_ui32 numIndex, r_ui32 offsetIndex, r_ui32 offsetVertex )
			{
				// 仮に逐一実行します
				r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext()->DrawIndexed ( numIndex, offsetIndex, offsetVertex );
			}


			void	ContextDX11 :: map2DTexture( r::draw::Texture2D* texture )
			{
				// 仮に逐一実行します
				Texture2DDX11* texdx11 = r::rtti::dynamicCast<Texture2DDX11*>(texture);
				RDEBUG_ASSERT( texdx11 );
				ID3D11DeviceContext* context = r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext();
				HRESULT res = context->Map( texdx11->getTexture(), 0, /*D3D11_MAP_WRITE*/D3D11_MAP_WRITE_DISCARD, 0, texdx11->getSubResource() );
				RDEBUG_ASSERTMSG( res == S_OK, _RST("result code %08x"), res );
			}

			void	ContextDX11 :: unmap2DTexture( r::draw::Texture2D* texture )
			{
				// 仮に逐一実行します
				Texture2DDX11* texdx11 = r::rtti::dynamicCast<Texture2DDX11*>( texture );
				RDEBUG_ASSERT( texdx11 );
				ID3D11DeviceContext* context = r::rtti::dynamicCast<CoreDX11*> ( r::draw::Core::getInstance() )->getD3DContext();
				context->Unmap( texdx11->getTexture(), 0 );
			}
		}
	}
}

#endif // R_TARGET_UNIQUEIS(R_TARGET_UNIQUE_WINDX11)


